//时间装字符
function timestampToTime(timestamp) {
        var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
        var Y = date.getFullYear() + '-';
        var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
        var D = date.getDate() + ' ';
        var h = date.getHours() + ':';
        var m = date.getMinutes() + ':';
        var s = date.getSeconds();
        return Y+M+D+h+m+s;
}


//字符串转时间
function timeStringToTime(timeString){

	date = timeString.substring(0,19);    
	date = date.replace(/-/g,'/'); 
	timestamp = new Date(date).getTime()/1000;
	return timestamp;
}


layui.use(['form','layer','table','laytpl','laydate'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table,
        laydate = layui.laydate;

	var carouselInfo=new Vue({
		el:"#carouselInfo",
		data:{
            all_data:null,
            tableIns:null
        },
        methods:{
        	//提交
        	sumbit:function(data){
        		
        		var url=global_url+'/admin/carousel/add'
				
				this.$http.post(url,data)
					.then(
					response=>{
						layer.msg(response.body.datas)
					},					
					response=>{
		
				})
					
        	},
        	
        	//更新
        	updata:function(data){
        		

				console.log(data)
				this.$http.post(global_url+'/admin/carousel/updata',data)
					.then(
					response=>{
						layer.msg(response.body.datas)
						parent.location.reload();
					},					
					response=>{
		
				})
        		
        	}
        
        }
		
	})
	

	setTimeout(function(){
			form.render(); 
				
		},500)
		
		



    laydate.render({
        elem: '#start',
        type: 'datetime'
    });
    laydate.render({
        elem: '#stop',
        type: 'datetime'
    });

	//提交按钮
	form.on("submit(changeCarousel)",function(data){
		var form_data=data.field;
		
		//添加
		if(form_data.id==""){
			let startTime=timeStringToTime(form_data.start);
			let stopTime=timeStringToTime(form_data.stop);
			if(startTime>stopTime){
				layer.msg("结束时间不能小于结束时间")
				return false;
			}
			
			let  sumbit_data={
				"image":form_data.img,
				"nid":form_data.novel_id,
				"startTime":startTime,
				"stopTime":stopTime
			}
			
			carouselInfo.sumbit(sumbit_data)
		}else if(form_data.id!=""){
			//更新
			let startTime=timeStringToTime(form_data.start);
			let stopTime=timeStringToTime(form_data.stop);
			if(startTime>stopTime){
				layer.msg("结束时间不能小于结束时间")
				return false;
			}
			
			let  updata_data={
				"id":form_data.id,
				"image":form_data.img,
				"nid":form_data.novel_id,
				"startTime":startTime,
				"stopTime":stopTime
			}
			
			carouselInfo.updata(updata_data)
		}
		
		return false
	});





})
